# !/user/bin/env python3
# -*- coding: utf-8 -*-
# -- anth bluedolphi ---

#生成AI油站日报表
import os

from util.UrlToPic import saveUrlToPic


def createHtmlAIYZDayReport(gasStationDateReport,noReportGasStationsForever,dataAlertNoDataGasStations):
    json =gasStationDateReport
    print("json = ", json)
    f1 = open('./spider/AIYZ/exportTable/aiGasRoport_mb.html', 'r+', encoding='UTF-8')
    f2 = open('./spider/AIYZ/exportTable/aiGasRoport.html', 'w+', encoding='UTF-8')
    headStr1 = "%headTitle%"
    headStr2 = '<tr><th width="50">%s</th><th width="250">%s</th><th width="80">%s</th><th width="80">%s</th><th width="80">会员数</th><th width="80">二级会员</th><th width="100' \
               '">总二级会员</th></tr>' % ('序号', '油站名称', (date + datetime.timedelta(days=-1)).strftime("%m-%d"),
                                      (date + datetime.timedelta(days=-2)).strftime("%m-%d"))

    rowsStr1 = "%row%"
    rows = json;
    rows.sort(key=lambda x: x["totalAmountToday"])

    # 删除不显示的加油站

    row = 1
    rowsStr2 = ''
    totalAmountToday = 0
    totalAmountYestoday = 0
    totalMemberToday = 0
    addSecondaryMemberToday = 0
    totalSecondaryMemberToday = 0
    index = 1  # 序号
    while row < len(rows):
        item = rows[len(rows) - row]
        row += 1
        # 如果是公司内容的帐号就不显示
        if item['stationName'] in noReportGasStationsForever:
            continue

        # 如果在不汇报的名单内，但是有数还是要汇报表
        if (item['stationName'] not in dataAlertNoDataGasStations) or (int(item['totalAmountToday']) > 0):
            rowsStr2 += "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr> " % (
            index, item['stationName'], item['totalAmountToday'], item['totalAmountYestoday'], item['totalMemberToday'],
            item['addSecondaryMemberToday'], item['totalSecondaryMemberToday'],)
            index += 1
            totalAmountToday += item['totalAmountToday']
            totalAmountYestoday += item['totalAmountYestoday']
            totalMemberToday += item['totalMemberToday']
            addSecondaryMemberToday += item['addSecondaryMemberToday']
            totalSecondaryMemberToday += item['totalSecondaryMemberToday']

    tFootStr1 = "%tfootRow%"
    tFootStr2 = "<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>" % (
    "", "合计", int(totalAmountToday), int(totalAmountYestoday), int(totalMemberToday), int(addSecondaryMemberToday),
    totalSecondaryMemberToday)
    for ss in f1.readlines():
        tt = re.sub(headStr1, headStr2, ss)
        tt = re.sub(rowsStr1, rowsStr2, tt)
        tt = re.sub(tFootStr1, tFootStr2, tt)
        f2.write(tt)

    f1.close()
    f2.close()
    # os.path.dirname(os.path.realpath(__file__)) = E:\python\AIGasRobot
    path = os.path.dirname(os.path.realpath(__file__));
    path = path.replace("\\", "/")

    url = "file:///" + path + "/exportTable/aiGasRoport.html"
    print(url)
    saveUrlToPic(url, reportfileName, element_id="content")